package com.fangzhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fangzhi.mapper.AdminAccountInfoMapper;
import com.fangzhi.mobel.AdminAccountInfo;
import com.fangzhi.mobel.Customer;
import com.fangzhi.mobel.enums.GlobalCodeEnum;
import com.fangzhi.mobel.io.SaveCustomerIo;
import com.fangzhi.mobel.vo.CustomerListVo;
import com.fangzhi.mobel.vo.LoginVo;
import com.fangzhi.mobel.vo.PageVo;
import com.fangzhi.service.CustomerService;
import com.fangzhi.mapper.CustomerMapper;
import com.fangzhi.utils.BeanCopyUtil;
import com.fangzhi.utils.GlobalParam;
import com.fangzhi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
* @author edy
* @description 针对表【customer(客户表)】的数据库操作Service实现
* @createDate 2024-09-12 12:45:17
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{

    @Autowired
    private AdminAccountInfoMapper adminAccountInfoMapper;
    @Override
    public ResponseResult saveCustomer(SaveCustomerIo io) {
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        Customer customer = BeanCopyUtil.copyProperties(io, Customer.class);
        customer.setAccountId(loginVo.getId()).setAddTime(null != io.getAddTime() ? io.getAddTime():LocalDate.now());
        boolean save = saveOrUpdate(customer);
        if (save)
            return ResponseResult.OK();
        throw new RuntimeException("添加客户失败" + JSON.toJSONString(io));
    }

    @Override
    public ResponseResult offlineCustomer(Integer id) {
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        Customer byId = this.getById(id);
        if (null == byId)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9996);

        Long accountId = GlobalParam.LOGIN_ACCOUNT_INFO.get().getId();
        if (byId.getAccountId() != accountId && loginVo.getRoleId() != 1)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9997);

        byId.setStatus(2);
        boolean b = this.updateById(byId);
        if (b)
            return ResponseResult.OK();
        throw new RuntimeException("下线客户失败" + JSON.toJSONString(id));
    }

    @Override
    public ResponseResult customerList(List<Long> accountIds, String keyword, Integer pageNo, Integer pageSize) {
        Long total = this.count(Wrappers.<Customer>lambdaQuery().and(wapper-> wapper.like(Customer::getHotelName, keyword).or().like(Customer::getName, keyword).or().like(Customer::getTotal, keyword))
                .eq(Customer::getStatus, 1)
                .in(Customer::getAccountId, accountIds));
        if (total == 0L)
            return ResponseResult.OK(new PageVo().setTotal(total).setData(new ArrayList<>()));
        List<Customer> list = this.list(Wrappers.<Customer>query().select(" id,name,hotel_name,total,account_id,comments,wx_id,province,city,area,descs,CONCAT(province,city,area,descs) as address,add_time").lambda().eq(Customer::getStatus, 1)
                .in(Customer::getAccountId, accountIds).and(wapper-> wapper.like(Customer::getHotelName, keyword).or().like(Customer::getName, keyword).or().like(Customer::getTotal, keyword)).last(" limit " + (pageNo - 1) * pageSize + "," + pageSize));
        List<CustomerListVo> copy = BeanCopyUtil.copy(list, CustomerListVo.class);
        for (CustomerListVo customerListVo : copy) {
            AdminAccountInfo adminAccountInfo = adminAccountInfoMapper.selectById(customerListVo.getAccountId());
            customerListVo.setAdminAccountName(adminAccountInfo.getName());
        }
        return ResponseResult.OK(new PageVo().setTotal(total).setData(copy));
    }
}




